﻿/**
 * Licensed to Open-Ones Group under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Open-Ones Group licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a
 * copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
namespace IPresenter
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Text;
    using System.Windows.Forms;
   
    /// <summary>
    /// This class use to customize shape of control
    /// </summary>
    public static class CustomizeControl
    {
        /// <summary>
        /// Change shape of control
        /// </summary>
        /// <param name="control">Instance of control</param>
        /// <param name="bitmap">Bitmap background</param>
        /// <returns>
        /// true: if success.
        /// false: if unsuccess.
        /// </returns>
        public static bool ChangeShapeControl(Control control, Bitmap bitmap)
        {
            if (control == null && bitmap == null)
            {
                return false;
            }

            control.Width = bitmap.Width;
            control.Height = bitmap.Height;
            if (control is System.Windows.Forms.Form)
            {
                const int BONUS_WIDTH = 15;
                const int BONUS_HEIGHT = 35;
                Form frmMainForm = (Form)control;
                frmMainForm.Width = control.Width + BONUS_WIDTH;
                frmMainForm.Height = control.Height + BONUS_HEIGHT;
                frmMainForm.BackgroundImage = bitmap;
                frmMainForm.Cursor = Cursors.Default;
                frmMainForm.FormBorderStyle = FormBorderStyle.None;
                frmMainForm.Region = new Region(CreateShapeControl(bitmap));
            }
            else
            {
                if (control is System.Windows.Forms.Button)
                {
                    Button btnControl = (Button)control;
                    btnControl.Text = string.Empty;
                    btnControl.BackgroundImage = bitmap;
                    btnControl.Cursor = Cursors.Hand;
                    btnControl.Region = new Region(CreateShapeControl(bitmap));
                }
                else
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Create shape for control
        /// </summary>
        /// <param name="bitmap">Bitmap background</param>
        /// <returns>
        /// null: if unsuccess.
        /// !=null: if success.
        /// </returns>
        public static GraphicsPath CreateShapeControl(Bitmap bitmap)
        {
            GraphicsPath graphics = new GraphicsPath();
            Color clTrans = bitmap.GetPixel(0, 0);
            for (int row = 0; row < bitmap.Height; row++)
            {
                for (int col = 0; col < bitmap.Width; col++)
                {
                    if (bitmap.GetPixel(col, row) != clTrans)
                    {
                        int temp = col;
                        for (temp = col; temp < bitmap.Width; temp++)
                        {
                            if (bitmap.GetPixel(temp, row) == clTrans)
                            {
                                break;
                            }
                        }

                        graphics.AddRectangle(new Rectangle(col, row, temp - col, 1));
                        col = temp;
                    }
                }
            }

            return graphics;
        }
    }
}
